﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Stratrgy
{
    internal class ProbStrategy : IStrategy
    {
        private Random random;
        private int previousHandValue;
        private int currentHandValue;
        private int[,] history = new int[,]
        {
            { 1, 1, 1,},
            { 1, 1, 1,},
            { 1, 1, 1,},
        };
        public ProbStrategy(int seed)
        {
            this.random = new Random(seed);
        }

        public Hand NextHand()
        {
            var bet = random.Next(this.GetSum(this.currentHandValue));
            var handValue = 0;
            if (bet < history[currentHandValue, 0])
            {
                handValue = 0;
            }
            else if (bet < history[currentHandValue, 0] + history[currentHandValue, 1])
            {
                handValue = 1;
            }
            else 
            {
                handValue = 2;
            }
            this.previousHandValue = this.currentHandValue;
            this.currentHandValue = handValue;
            return Hand.GetHand(handValue);
        }

        private int GetSum(int handValue) 
        {
            var sum = 0;
            for (var i = 0; i < 3; i++) 
            {
                sum += history[handValue,i];
            }
            return sum;
        }

        public void Study(bool win)
        {
            if (win)
            {
                history[previousHandValue, currentHandValue]++;
            }
            else 
            {
                history[previousHandValue, (currentHandValue + 1) % 3]++;
                history[previousHandValue, (currentHandValue + 2) % 3]++;
            }
        }
    }
}
